
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            {{# if(d.status==0){}}
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="allow" permission="app.admin.rlorderinvoice.allow">审核通过</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="refuse" permission="app.admin.rlorderinvoice.refuse">审核拒绝</button>
            {{# } }}
            {{# if(d.status==1){}}
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="update" permission="app.admin.rlorderinvoice.update">确认开票</button>
            {{# } }}

            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.rlorderinvoice.delete">删除</button>
        </script>

        <script type="text/html" id="createTime">
            {{# if(d.create_time>0){}}
            {{layui.util.toDateString(d.create_time+'000', 'yyyy-MM-dd HH:mm:ss')}}
            {{# } }}
        </script>

        <script type="text/html" id="updateTime">
            {{# if(d.update_time>0){}}
            {{layui.util.toDateString(d.update_time+'000', 'yyyy-MM-dd HH:mm:ss')}}
            {{# } }}
        </script>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/rl-order-invoice/select";
            const UPDATE_API = "/app/admin/rl-order-invoice/update";
            const DELETE_API = "/app/admin/rl-order-invoice/delete";
            const INSERT_URL = "/app/admin/rl-order-invoice/insert";
            const UPDATE_URL = "/app/admin/rl-order-invoice/update";
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						title: "ID",align: "left",
						field: "id",
                        width: '80',
					},{
						title: "类型",align: "left",
						field: "type",
                        width: '80',
                        templet: function (d) {
                            //1公司，2个人
                            let css = {"1":"layui-bg-green","2":"layui-bg-blue"}[d["type"]];
                            let title = {"1":"公司","2":"个人"}[d["type"]];
                            return '<span class="layui-badge '+css+'">'+util.escape(title)+'</span>';
                        }
					},{
						title: "订单号",align: "left",
						field: "order_sn",
                        width: '150',
					},{
						title: "用户ID",align: "left",
						field: "member_id",
                        width: '80',
                        hide:true,
					},{
                        title: "用户昵称",align: "left",
                        field: "nickname",
                        width: '120',
                    },{
                        title: "用户头像",align: "left",
                        field: "avatar",
                        width: '120',
                        templet: function (d) {
                            return '<img src="'+encodeURI(d['avatar'])+'" style="max-width:32px;max-height:32px;" alt="" />'
                        }
                    },{
						title: "状态",align: "left",
						field: "status",
                        width: '100',
                        templet: function (d) {
                            //0待审核，1审核通过，2审核失败，3已开具，9已取消
                            let css = {"0":"layui-bg-warn","1":"layui-bg-green","3":"layui-bg-blue","2":"layui-bg-red","9":"layui-bg-warn"}[d["status"]];
                            let title = {"0":"待审核","1":"审核通过","2":"审核未通过","3":"已开具","9":"已取消"}[d["status"]];
                            return '<span class="layui-badge '+css+'">'+util.escape(title)+'</span>';
                        }
					},{
						title: "发票抬头",align: "left",
						field: "company_name",
                        width: '150',
					},{
						title: "税号",align: "left",
						field: "company_number",
                        width: '200',
					},{
						title: "公司电话",align: "left",
						field: "company_tel",
                        width: '120',
					},{
						title: "开户银行",align: "left",
						field: "company_open_bank",
                        width: '120',
					},{
						title: "银行账号",align: "left",
						field: "company_open_bank_number",
                        width: '120',
					},{
                        title: "企业地址",align: "left",
                        field: "company_address",
                        width: '200',
                    },{
						title: "申请人姓名",align: "left",
						field: "consignee_name",
                        width: '120',
					},{
						title: "申请人电话",align: "left",
						field: "consignee_mobile",
                        width: '120',
					},{
						title: "申请人地址",align: "left",
						field: "consignee_address",
                        width: '120',
					},{
						title: "创建时间",align: "left",
						field: "create_time",
                        width: '150',
					},{
						title: "更新时间",align: "left",
						field: "update_time",
                        width: '150',
					},{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 250,
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function () {
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});
				
				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "update") {
                        edit(obj);
                    } else if (obj.event === "allow") {
                        allow(obj);
                    } else if (obj.event === "refuse") {
                        refuse(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    table.reload("data-table", {
                        where: []
                    })
                });
                
                // 字段允许为空
                form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    return doUpdate(value,3)
                }

                let allow = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    return doUpdate(value,1)
                }

                let refuse = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    return doUpdate(value,2)
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                //执行更新
                let doUpdate = function (id,status) {
                    let data = {};
                    data[PRIMARY_KEY] = id;
                    data['status'] = status;
                    let loading = layer.load();
                    $.ajax({
                        url: UPDATE_API,
                        data: data,
                        dataType: "json",
                        type: "post",
                        success: function(res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", refreshTable);
                        }
                    })
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }
            })

        </script>
    </body>
</html>
